var tableDataCombo = new Ext.form.ComboBox({
	id : 'table-data-combo',
	name : 'table-data-combo',
	store : new Ext.data.SimpleStore({
		fields : [ 'name' ],
		data : [ [ 'domainmodel' ],
		         [ 'domainmodelattr' ],
				 [ 'domainmodelstate' ],
				 [ 'epcunit' ],
				 [ 'epcunits' ],
				 [ 'fileinfo' ],
				 [ 'functions' ],
				 [ 'parameter' ],
				 [ 'permission' ],
				 [ 'permisssionresources' ],
				 [ 'process' ],
				 [ 'resource' ],
				 [ 'role' ],
				 [ 'rolepermissions' ],
				 [ 'subprocess' ],
				 ]
	}),
	valueField : "name",
	displayField : "name",
	editable : false,
	forceSelection : true,
	typeAhead : true,
	triggerAction : 'all',
	selectOnFocus : true,// 用户不能自己输入,只能选择列表中有的记录
	allowBlank : false,
	mode : 'local',
	listeners : {
		select : function() {
			var tn=tableDataCombo.getValue();
			dataColumnStore.load({ params: { tablename: tn},callback:function(){
				valueList.getStore().reader=buildGridStoreReader(dataColumnStore);
				valueList.reconfigure(valueList.getStore(),buildGridColumnModel(dataColumnStore));
				valueStore.load({ params: { tablename: tn} ,callback:function(){
					 formatValue(dataColumnStore,valueStore,recordStore);
				}});				
			} });			
		},
		afterRender : function(combo) {
			tableDataCombo.setValue('domainmodel');//同时下拉框会将与name为firstValue值对应的 text显示
		}
	}
});

var dataColumnStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showTableColumn.action?method=showTableColumn',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "column",
		totalProperty : "total",
		id : "columnname"
	}, [ {
		name : 'id'
	}, {
		name : 'columnname'
	}, {
		name : 'tablename'
	}, {
		name : 'type'
	} ])
});

//动态构建表格数据模型
function buildGridStoreReader(cstore){
	var cols=new Array();
	cols.push({
		name:"id"
	});
	cstore.each(function(record){
		cols.push({
			name:record.get('columnname')
		});
	});
	var reader=new Ext.data.JsonReader({
		root : "columnValue",
		totalProperty : "total",
		id : "id"
	}, cols);
	return reader;
}

//动态构建表格列模型
function buildGridColumnModel(cstore){
	var cols=new Array();
	cols.push(new Ext.grid.RowNumberer());
	cols.push({
		header : 'ID',
		width : 100,
		dataIndex : 'id',
		sortable : true,
		id : 'id'
	});
	cstore.each(function(record){
		cols.push({
			header : record.get('columnname'),
			width : 100,
			dataIndex : 'id',
			sortable : true,
			id : record.get('columnname')
		});
	});
	
	var columnModel = new Ext.grid.ColumnModel(cols);
	return columnModel;
}


var recordStore = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showTableValue.action?method=showTableValue',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "columnValue",
		totalProperty : "total",
		id : "columnname"
	}, [ {
		name : 'id'
	}, {
		name : 'columnname'
	}, {
		name : 'tablename'
	}, {
		name : 'type'
	} ])
});

var valueList = new Ext.grid.GridPanel({
	enableColumnResize : true,
	border : true,
	clicksToEdit : 1,
	enableColumnMove : true,
	enableHdMenu : false,
	stripeRows : true, //显示斑马线
	loadMask : true,
	autoHeight : true,
	bodyStyle : 'width:100%',
	autoWidth : true,
	editable : true,
	cm : columnModel,
	store : recordStore,
	tbar:[
	      '选择数据表：',
	      tableDataCombo	      
	]
});

var tablemgr = new Ext.Panel({
	title : '数据管理',
	id : 'data-manager-panel',
	border : 'fit',
	items : [
	          valueList],
	showPage : function() {
		var tn=tableDataCombo.getValue();
		dataColumnStore.load({ params: { tablename: tn},callback:function(){
			valueList.getStore().reader=buildGridStoreReader(dataColumnStore);
			valueList.reconfigure(valueList.getStore(),buildGridColumnModel(dataColumnStore));
			valueStore.load({ params: { tablename: tn} ,callback:function(){
				 formatValue(dataColumnStore,valueStore,recordStore);
			}});
		} });		
	}
});

var valueStore=new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : 'showTableValue.action?method=showTableValue',
		method : 'post'
	}),
	reader : new Ext.data.JsonReader({
		root : "columnValue",
		totalProperty : "total",
		id : "id"
	}, [ {
		name : 'id'
	}, {
		name : 'columnId'
	}, {
		name : 'value'
	}, {
		name : 'recordId'
	} ])
});

function formatValue(cstore,vstore,rstore){
	var cols=new Array();
	var colmap=new Array();
	cols.push({name:'id'});
	cstore.each(function(record){
		cols.push({
			name:record.get('columnname')
		});
		colmap[record.get('id')]=record.get('columnname');		
	});
	
	MyRecord=Ext.data.Record.create(cols);	
	var records=new Array();
	vstore.each(function(record){
		var rec=records[record.get('id')];
		if(rec==null){
			records[record.get('id')]=new Array();
			rec=records[record.get('id')];
			rec['id']=record.get('id');
		}		
		rec[colmap[record.get('id')]]=record.get('value');
	});
	
	for(var recid in records){
		var r=new MyRecord();
		for(var key in records[recid]){
			r.set(key,records[recid][key]);
		}
		rstore.add(r);
	}
}
